//
//  CouponFormItem.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/8.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "CouponFormItem.h"
//static NSString *const SWUnitYuan = JMLocalStr(@"元") ;
//static NSString *const SWUnitPieces = @"万元";
@interface CouponFormItem()

+ (instancetype)jm_itemWithTitle:(NSString *)title info:(NSString *)info itemType:(CouponFormItemType)itemType editable:(BOOL)editable required:(BOOL)required placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)keyboardTyp;

@end
inline CouponFormItem *couponItem_Add(NSString * _Nonnull title, NSString *info,NSString *placeHolder, CouponFormItemType itemType, BOOL editable, BOOL required, UIKeyboardType keyboardType) {
    return [CouponFormItem jm_itemWithTitle:title info:info itemType:itemType editable:editable required:required placeHolder:placeHolder keyboardType:keyboardType];
}

@implementation CouponFormItem
+ (instancetype)jm_itemWithTitle:(NSString *)title info:(NSString *)info itemType:(CouponFormItemType)itemType editable:(BOOL)editable required:(BOOL)required placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)keyboardType {
    return [[self alloc]initWithTitle:title info:info itemType:itemType editable:editable required:required placeHolder:placeHolder keyboardType:keyboardType showPlaceholder:YES];
}

- (instancetype)initWithTitle:(NSString *)title info:(NSString *)info itemType:(CouponFormItemType)itemType editable:(BOOL)editable required:(BOOL)required placeHolder:(NSString *)placeHolder keyboardType:(UIKeyboardType)keyboardType showPlaceholder:(BOOL)showPlaceholder{
    self = [super init];
    if (self) {
        self.itemUnitType = CouponFormItemTypeNormal;
        self.title = title;
        self.info = info;
        self.itemType = itemType;
        self.editable = editable;
        self.required = required;
        self.keyboardType = keyboardType;
        self.placeholder = placeHolder;
        self.showPlaceholder = showPlaceholder;
        [self jm_setDefaultHeight:itemType];
        [self jm_setPlaceholderWithShow:showPlaceholder itemType:itemType];
        [self jm_setAttributedTitleWithRequired:required title:title itemType:itemType];
    }
    return self;
}

#pragma mark -- 根据表单条目类型设置条目缺省高度
- (void)jm_setDefaultHeight:(CouponFormItemType)itemType {
    self.defaultHeight = 60;
}

#pragma mark -- 设置是否显示输入框占位字符
- (void)jm_setPlaceholderWithShow:(BOOL)show itemType:(CouponFormItemType)itemType {
//    if (!show) {
//        self.placeholder = @"";
//        return;
//    }
//    switch (itemType) {
//        case CouponFormItemTypeNormal:
//        {
//            self.placeholder = @"请输入";
//        }
//            break;
//        case CouponFormItemTypeTimeSelected:
//        {
//            self.placeholder = @"请选择 >";
//        }
//            break;
//        default:
//            self.placeholder = @"";
//            break;
//    }
}

#pragma mark -- 设置标题显示
- (void)jm_setAttributedTitleWithRequired:(BOOL)required title:(NSString *)title itemType:(CouponFormItemType)itemType{
    if (required) {
        title = [NSString stringWithFormat:@"＊%@", title];
    }
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc]initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorHex(333333)}];
    if (required) {
        [attributedTitle addAttribute:NSForegroundColorAttributeName value:UIColorHex(FC3E39) range:NSMakeRange(0, 1)];
    }
    _attributedTitle = attributedTitle;
}

#pragma mark -- 重写属性set方法，防止单独改变属性无响应效果
/**
 设置表单条目附带单位
 */
- (void)setItemUnitType:(CouponFormItemUnitType)itemUnitType {
    NSString *tempUnit = self.unit ?: @"";
    switch (itemUnitType) {
        case CouponFormItemUnitTypeNone:
        {
            tempUnit = @"";
        }
            break;
        case CouponFormItemUnitTypeYuan:
        {
            tempUnit = [NSString stringWithFormat:@"(%@)",JMLocalStr(@"元")];
        }
            break;
        case CouponFormItemUnitTypePieces:
        {
            tempUnit = [NSString stringWithFormat:@"(%@)",JMLocalStr(@"张")];
        }
            break;
        default:
            break;
    }
    _unit = tempUnit;
}

/**
 根据单位设置单元格单位类别，防止单位与单元格式不一致
 */
- (void)setUnit:(NSString *)unit {
    _unit = unit;
    if ([unit isEqualToString:@""]) {
        _itemUnitType = CouponFormItemUnitTypeNone;
    }
    else if (unit == [NSString stringWithFormat:@"(%@)",JMLocalStr(@"元")]) {
        _itemUnitType = CouponFormItemUnitTypeYuan;
    }
    else if (unit == [NSString stringWithFormat:@"(%@)",JMLocalStr(@"张")]) {
        _itemUnitType = CouponFormItemUnitTypePieces;
    }
    else {
        _itemUnitType = CouponFormItemUnitTypeCustom;
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [self jm_setAttributedTitleWithRequired:self.required title:title itemType:self.itemType];
}

- (void)setRequired:(BOOL)required {
    _required = required;
    [self jm_setAttributedTitleWithRequired:required title:self.title itemType:self.itemType];
}

- (void)setItemType:(CouponFormItemType)itemType {
    _itemType = itemType;
    [self jm_setDefaultHeight:itemType];
    [self jm_setAttributedTitleWithRequired:self.required title:self.title itemType:itemType];
    [self jm_setPlaceholderWithShow:self.showPlaceholder itemType:itemType];
}

- (void)setShowPlaceholder:(BOOL)showPlaceholder {
    _showPlaceholder = showPlaceholder;
    [self jm_setPlaceholderWithShow:showPlaceholder itemType:self.itemType];
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc]initWithString:placeholder ?: @"" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:UIColorHex(B2B2B2)}];
    _attributedPlaceholder = attributedPlaceholder;
}

- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder {
    _attributedPlaceholder = attributedPlaceholder ?: [[NSAttributedString alloc]initWithString:@""];
}
@end
